package com.skyd.core.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Menu;
import android.view.MenuItem;

/**
 * 封装了通用功能的Activity
 * 
 * @author SkyD
 * 
 */
public class CommonActivity extends BaseActivity {
	
	/**
	 * 设置软件是否已注册（用于激活程序中调用）
	 * @param sharedPreferencesKey 共享参数Key
	 * @param valueKey 值Key
	 * @param value 值
	 */
	public void setIsRegistered(String sharedPreferencesKey,String valueKey,boolean value){
		Android.getSharedPreferences(this, sharedPreferencesKey,
				MODE_WORLD_READABLE).edit().putBoolean(valueKey, value)
				.commit();
	}
	
	/**
	 * 获取软件是否已注册（用于主程序中调用）
	 * @param targetPackage 目标（激活程序）包名
	 * @param sharedPreferencesKey 共享参数Key
	 * @param valueKey 值Key
	 * @return 软件是否已注册
	 */
	public Boolean getIsRegistered(String targetPackage,String sharedPreferencesKey,String valueKey){
		Context otherAppsContext = null;
		  try
		  {
		   otherAppsContext = createPackageContext(
				   targetPackage, 0);
		  }
		  catch (NameNotFoundException e)
		  {
			  return false;
		  }
		 SharedPreferences mPrefs = otherAppsContext.getSharedPreferences(sharedPreferencesKey,Context.MODE_PRIVATE);
		 return mPrefs.getBoolean(valueKey, false);
	}

	int openUrlDialogUrlRes;

	/**
	 * 显示确认打开网址对话框
	 * 
	 * @param titleResID
	 *            标题字串资源ID
	 * @param msgResID
	 *            内容字串资源ID
	 * @param yesResID
	 *            ‘是’字串资源ID
	 * @param noResID
	 *            ‘否’字串资源ID
	 * @param urlResID
	 *            网址字串资源ID
	 */
	public void openUrlDialog(int titleResID, int msgResID, int yesResID,
			int noResID, int urlResID) {
		openUrlDialogUrlRes = urlResID;
		new AlertDialog.Builder(this).setIcon(android.R.drawable.btn_star)
				.setTitle(titleResID).setMessage(msgResID).setPositiveButton(
						yesResID, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Android.openUrl(CommonActivity.this,
										openUrlDialogUrlRes);
							}
						}).setNegativeButton(noResID, null).create().show();
	}

	/**
	 * 设置查看演示菜单
	 * 
	 * @param menu
	 *            菜单
	 * @param order
	 *            顺序
	 * @param title
	 *            标题
	 * @param urlResID
	 *            网址资源ID
	 */
	public void setViewDemoMenu(Menu menu, int order, String title, int urlResID) {
		setViewDemoMenuUrlResID = urlResID;
		menu.add(Menu.NONE, Integer.MAX_VALUE - 1, order, title);
	}

	/**
	 * 设置查看演示菜单
	 * 
	 * @param menu
	 *            菜单
	 * @param order
	 *            顺序
	 * @param titleResID
	 *            标题资源ID
	 * @param urlResID
	 *            网址资源ID
	 */
	public void setViewDemoMenu(Menu menu, int order, int titleResID,
			int urlResID) {
		setViewDemoMenuUrlResID = urlResID;
		menu.add(Menu.NONE, Integer.MAX_VALUE - 1, order, titleResID);
	}

	int setViewDemoMenuUrlResID;

	/**
	 * 设置更多程序菜单
	 * 
	 * @param menu
	 *            菜单
	 * @param order
	 *            顺序
	 * @param titleResID
	 *            标题资源ID
	 * @param authorNameResID
	 *            作者名称资源ID
	 */
	public void setMoreAppMenu(Menu menu, int order, int titleResID,
			int authorNameResID) {
		setMoreAppMenuAuthorNameResID = authorNameResID;
		menu.add(Menu.NONE, Integer.MAX_VALUE - 2, order, titleResID);
	}

	/**
	 * 设置更多程序菜单
	 * 
	 * @param menu
	 *            菜单
	 * @param order
	 *            顺序
	 * @param title
	 *            标题
	 * @param authorNameResID
	 *            作者名称资源ID
	 */
	public void setMoreAppMenu(Menu menu, int order, String title,
			int authorNameResID) {
		setMoreAppMenuAuthorNameResID = authorNameResID;
		menu.add(Menu.NONE, Integer.MAX_VALUE - 2, order, title);
	}

	int setMoreAppMenuAuthorNameResID;

	/**
	 * 设置获取完全版本菜单
	 * 
	 * @param menu
	 *            菜单
	 * @param order
	 *            顺序
	 * @param title
	 *            标题
	 * @param urlResID
	 *            网址资源ID
	 */
	public void setGetFullVersionMenu(Menu menu, int order, String title,
			int urlResID) {
		setGetFullVersionMenuUrlResID = urlResID;
		menu.add(Menu.NONE, Integer.MAX_VALUE - 3, order, title);
	}

	/**
	 * 设置获取完全版本菜单
	 * 
	 * @param menu
	 *            菜单
	 * @param order
	 *            顺序
	 * @param titleResID
	 *            标题资源ID
	 * @param urlResID
	 *            网址资源ID
	 */
	public void setGetFullVersionMenu(Menu menu, int order, int titleResID,
			int urlResID) {
		setGetFullVersionMenuUrlResID = urlResID;
		menu.add(Menu.NONE, Integer.MAX_VALUE - 3, order, titleResID);
	}

	int setGetFullVersionMenuUrlResID;

	/**
	 * 菜单处理函数
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		int id = item.getItemId();
		if (id == Integer.MAX_VALUE - 1) {
			Android.openUrl(this, setViewDemoMenuUrlResID);
		} else if (id == Integer.MAX_VALUE - 2) {
			Android.openUrl(this, "http://market.android.com/search?q=pub:"
					+ getResources().getString(setMoreAppMenuAuthorNameResID));
		} else if (id == Integer.MAX_VALUE - 3) {
			Android.openUrl(this, setGetFullVersionMenuUrlResID);
		}
		return super.onMenuItemSelected(featureId, item);
	}
	
	/**
	 * 打开一个Activity
	 * @param targetclass 目标Activity的类型
	 */
	public void openActivity(Class<? extends Activity> targetclass){
		openActivity(new Intent(),targetclass);
	}
	
	/**
	 * 打开一个Activity
	 * @param intent 传递的Intent
	 * @param targetclass 目标Activity的类型
	 */
	public void openActivity(Intent intent,Class<? extends Activity> targetclass){
		intent.setClass(CommonActivity.this, targetclass);
			CommonActivity.this.startActivity(intent);
	}
}
